package com.mogist.hqc.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.mogist.hqc.R;
import com.mogist.hqc.util.Constant;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import talex.zsw.basecore.util.LogTool;
import talex.zsw.basecore.view.dialog.sweetalertdialog.SweetAlertDialog;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler
{
	private IWXAPI api;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wxpay);
		api = WXAPIFactory.createWXAPI(this, Constant.WX_APP_ID);

		api.handleIntent(getIntent(), this);
		EventBus.getDefault().register(this);
	}

	@Override
	protected void onNewIntent(Intent intent)
	{
		super.onNewIntent(intent);
		setIntent(intent);
		api.handleIntent(intent, this);
	}

	@Override protected void onDestroy()
	{
		EventBus.getDefault().unregister(this);
		super.onDestroy();
	}

	@Override
	public void onReq(BaseReq req)
	{
	}

	@Override public void onBackPressed()
	{
	}

	@Override
	public void onResp(BaseResp resp)
	{
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX)
		{
			int code = resp.errCode;
			LogTool.nd("支付返回code = " + resp.errCode);
			String msg = "";
			switch (code)
			{
				case 0:
					msg = "支付成功！";
					new SweetAlertDialog(WXPayEntryActivity.this, SweetAlertDialog.SUCCESS_TYPE)
						.setTitleText(msg).setConfirmText("确定").setConfirmClickListener(
						new SweetAlertDialog.OnSweetClickListener()
						{
							@Override public void onClick(SweetAlertDialog sweetAlertDialog)
							{
								sweetAlertDialog.dismiss();
								EventBus.getDefault().post(Constant.EVENT_REFRESH_ORDER);
								EventBus.getDefault().post(Constant.EVENT_PAY_SUCCESS);
								WXPayEntryActivity.this.finish();
							}
						})
						.show();
					break;
				case -1:
					msg = "支付失败！";
					new SweetAlertDialog(WXPayEntryActivity.this, SweetAlertDialog.ERROR_TYPE)
						.setTitleText(msg).setConfirmText("确定").setConfirmClickListener(
						new SweetAlertDialog.OnSweetClickListener()
						{
							@Override public void onClick(SweetAlertDialog sweetAlertDialog)
							{
								sweetAlertDialog.dismiss();
								setResult(10001);
								WXPayEntryActivity.this.finish();
								EventBus.getDefault().post(Constant.EVENT_PAY_FAILED);
							}
						})
						.show();
					break;
				case -2:
					msg = "您取消了支付！";
					new SweetAlertDialog(WXPayEntryActivity.this, SweetAlertDialog.WARNING_TYPE)
						.setTitleText(msg).setConfirmText("确定").setConfirmClickListener(
						new SweetAlertDialog.OnSweetClickListener()
						{
							@Override public void onClick(SweetAlertDialog sweetAlertDialog)
							{
								sweetAlertDialog.dismiss();
								setResult(10002);
								WXPayEntryActivity.this.finish();
								EventBus.getDefault().post(Constant.EVENT_PAY_FAILED);
							}
						})
						.show();
					break;
				default:
					msg = "未知原因支付失败！";
					new SweetAlertDialog(WXPayEntryActivity.this, SweetAlertDialog.ERROR_TYPE)
						.setTitleText(msg).setConfirmText("确定").setConfirmClickListener(
						new SweetAlertDialog.OnSweetClickListener()
						{
							@Override public void onClick(SweetAlertDialog sweetAlertDialog)
							{
								sweetAlertDialog.dismiss();
								setResult(10003);
								WXPayEntryActivity.this.finish();
								EventBus.getDefault().post(Constant.EVENT_PAY_FAILED);
							}
						})
						.show();
					break;
			}
		}
	}

	// ======================== EventBus ====================
	@Subscribe
	public void onEvent(String event){
	}
}